using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Text;



/// <summary>
/// Summary description for Common
/// </summary>
public class Common
{
    public static int accCodeValue = 0;
    public static int accTypeValue = 0;
    public static string accCodeName = "";
    public static string accTypeName = "";

    public static int stkCodeValue = 0;
    public static int stkTypeValue = 0;
    public static string stkCodeName = "";
    public static string stkTypeName = "";
    public static int stkPrice = 0;
    public static int stkAccCode = 0; 
    public static int stkAccType = 0;

    public static int month = 1;
    public static int year = 2011;
    public static int DD1 = 0;
    public static int DD2 = 0;

    public Common()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public static void Getdata(DataSet ds,string query,string tbname)
    {

        string con = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

 //       string con = @"Provider=SQLSql;Data Source=CSE-PC\SQLEXPRESS;User ID=sa;Initial Catalog=DB2011;Password=1234";
        
        StringBuilder sv = new StringBuilder(con);

        SqlConnection _oleConn = new SqlConnection();    
        _oleConn.ConnectionString = sv.ToString();
        //string cmd1 = query;

        SqlCommand comm = new SqlCommand(query, _oleConn);

        SqlDataAdapter da = new SqlDataAdapter();
        //DataSet ds = new DataSet();

        da.SelectCommand = comm;
        da.Fill(ds, tbname);
        //return ds;
    }

    public static int GetRefn(string nature, string year)
    {
        int intReturn = 0;
        try
        {
            string con = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
            //string con = @"Provider=SQLSql;Data Source=CSE-PC\SQLEXPRESS;User ID=sa;Initial Catalog=DB2011;Password=1234";
            StringBuilder sv = new StringBuilder(con);

            // Get a voucher number for selected voucher for the selected year

            SqlConnection _oleConn = new SqlConnection();
            _oleConn.ConnectionString = sv.ToString();
            string cmd1 = "SELECT MAX(REFN)+1 FROM TRADE where NATURE='" + nature + "' and year(date)=" + year;


            SqlCommand comm = new SqlCommand(cmd1, _oleConn);

            SqlDataAdapter da2 = new SqlDataAdapter();
            DataSet ds2 = new DataSet();

            da2.SelectCommand = comm;
            da2.Fill(ds2, "maxrefn");

            if (ds2.Tables["maxrefn"].Rows[0][0].ToString().Equals(string.Empty))
            {
                intReturn = 1;
            }
            else
            {
                intReturn = Int32.Parse(ds2.Tables["maxrefn"].Rows[0][0].ToString());
            }
        }
        catch (Exception ex)
        {
            
            throw ex;
        }

        return intReturn;
    }


    public static Boolean IsRightIntegerEntry(string value)
    {
        int amount = 0;
        if (!Int32.TryParse(value, out amount))
              return false;
        if (amount < 0)
              return false;
          return true;
    }

    public static Boolean IsRightDecimalEntry(string value)
    {
        decimal amount = 0;
        
        if (!decimal.TryParse(value, out amount))
            return false;
        if (amount < 0)
            return false;
        return true;
    }
    
    public static int GetItemPrice(int itype, int icode)
    {
        int p;
        DataSet ds = new DataSet();
        Common.Getdata(ds, "select price from stock where itype=" + itype.ToString() + "and icode=" + icode.ToString(), "prc");
        if (ds.Tables["prc"].Rows.Count > 0)
            p = Convert.ToInt32(ds.Tables["prc"].Rows[0][0].ToString());
        else
            p = 0;
        return p;
    }
}
